﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.CommandUIDefinitionClasses
{
    public class GalleryButton : ICommandUIDefinition, IControlsChild, IXmlBuildable
    {
        public string Alt { get; set; }
        public string Command { get; set; }
        public string CommandPreview { get; set; }
        public string CommandRevert { get; set; }
        public Enumerations.CommandType CommandType { get; set; }
        public string CommandValueId { get; set; }
        public Enumerations.ElementDimensions ElementDimensions { get; set; }
        public string Id { get; set; }
        public string Image { get; set; }
        public string ImageClass { get; set; }
        public int? ImageLeft { get; set; }
        public int? ImageTop { get; set; }
        public string InnerHTML { get; set; }
        public string MenuItemId { get; set; }
        public string QueryCommand { get; set; }
        public int? Sequence { get; set; }
        public string TemplateAlias { get; set; }
        public string ToolTipDescription { get; set; }
        public string ToolTipHelpKeyWord { get; set; }
        public string ToolTipImage32by32 { get; set; }
        public string ToolTipImage32by32Class { get; set; }
        public int? ToolTipImage32by32Left { get; set; }
        public int? ToolTipImage32by32Top { get; set; }
        public string ToolTipShortcutKey { get; set; }
        public string ToolTipTitle { get; set; }

        public GalleryButton()
        { }

        public GalleryButton(XmlNode node)
            : this()
        {
            try
            {
                this.Alt = XmlHelper.GetAttribute(node, "Alt");
                this.Command = XmlHelper.GetAttribute(node, "Command");
                this.CommandPreview = XmlHelper.GetAttribute(node, "CommandPreview");
                this.CommandRevert = XmlHelper.GetAttribute(node, "CommandRevert");
                this.CommandType = (Enumerations.CommandType)Enum.Parse(typeof(Enumerations.CommandType), XmlHelper.GetAttribute(node, "CommandType"));
                this.CommandValueId = XmlHelper.GetAttribute(node, "CommandValueId");
                this.ElementDimensions = (Enumerations.ElementDimensions)Enum.Parse(typeof(Enumerations.ElementDimensions), XmlHelper.GetAttribute(node, "ElementDimensions"));
                this.Id = XmlHelper.GetAttribute(node, "Id");
                this.Image = XmlHelper.GetAttribute(node, "Image");
                this.ImageClass = XmlHelper.GetAttribute(node, "ImageClass");
                this.ImageLeft = XmlHelper.GetNullableIntAttribute(node, "ImageLeft");
                this.ImageTop = XmlHelper.GetNullableIntAttribute(node, "ImageTop");
                this.InnerHTML = XmlHelper.GetAttribute(node, "InnerHTML");
                this.MenuItemId = XmlHelper.GetAttribute(node, "MenuItemId");
                this.QueryCommand = XmlHelper.GetAttribute(node, "QueryCommand");
                this.Sequence = XmlHelper.GetNullableIntAttribute(node, "Sequence");
                this.TemplateAlias = XmlHelper.GetAttribute(node, "TemplateAlias");
                this.ToolTipDescription = XmlHelper.GetAttribute(node, "ToolTipDescription");
                this.ToolTipHelpKeyWord = XmlHelper.GetAttribute(node, "ToolTipHelpKeyWord");
                this.ToolTipImage32by32 = XmlHelper.GetAttribute(node, "ToolTipImage32by32");
                this.ToolTipImage32by32Class = XmlHelper.GetAttribute(node, "ToolTipImage32by32Class");
                this.ToolTipImage32by32Left = XmlHelper.GetNullableIntAttribute(node, "ToolTipImage32by32Left");
                this.ToolTipImage32by32Top = XmlHelper.GetNullableIntAttribute(node, "ToolTipImage32by32Top");
                this.ToolTipShortcutKey = XmlHelper.GetAttribute(node, "ToolTipShortcutKey");
                this.ToolTipTitle = XmlHelper.GetAttribute(node, "ToolTipTitle");
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode commandUINode)
        {
            try
            {
                XmlNode galleryButtonNode = XmlHelper.AddNode(commandUINode, "GalleryButton");
                XmlHelper.AddAttribute(galleryButtonNode, "Alt", this.Alt);
                XmlHelper.AddAttribute(galleryButtonNode, "Command", this.Command);
                XmlHelper.AddAttribute(galleryButtonNode, "CommandPreview", this.CommandPreview);
                XmlHelper.AddAttribute(galleryButtonNode, "CommandRevert", this.CommandRevert);
                XmlHelper.AddAttribute(galleryButtonNode, "CommandType", this.CommandType);
                XmlHelper.AddAttribute(galleryButtonNode, "CommandValueId", this.CommandValueId);
                XmlHelper.AddAttribute(galleryButtonNode, "ElementDimensions", this.ElementDimensions);
                XmlHelper.AddAttribute(galleryButtonNode, "Id", this.Id);
                XmlHelper.AddAttribute(galleryButtonNode, "Image", this.Image);
                XmlHelper.AddAttribute(galleryButtonNode, "ImageClass", this.ImageClass);
                XmlHelper.AddAttribute(galleryButtonNode, "ImageLeft", this.ImageLeft);
                XmlHelper.AddAttribute(galleryButtonNode, "ImageTop", this.ImageTop);
                XmlHelper.AddAttribute(galleryButtonNode, "InnerHTML", this.InnerHTML);
                XmlHelper.AddAttribute(galleryButtonNode, "MenuItemId", this.MenuItemId);
                XmlHelper.AddAttribute(galleryButtonNode, "QueryCommand", this.QueryCommand);
                XmlHelper.AddAttribute(galleryButtonNode, "Sequence", this.Sequence);
                XmlHelper.AddAttribute(galleryButtonNode, "TemplateAlias", this.TemplateAlias);
                XmlHelper.AddAttribute(galleryButtonNode, "ToolTipDescription", this.ToolTipDescription);
                XmlHelper.AddAttribute(galleryButtonNode, "ToolTipHelpKeyWord", this.ToolTipHelpKeyWord);
                XmlHelper.AddAttribute(galleryButtonNode, "ToolTipImage32by32", this.ToolTipImage32by32);
                XmlHelper.AddAttribute(galleryButtonNode, "ToolTipImage32by32Class", this.ToolTipImage32by32Class);
                XmlHelper.AddAttribute(galleryButtonNode, "ToolTipImage32by32Left", this.ToolTipImage32by32Left);
                XmlHelper.AddAttribute(galleryButtonNode, "ToolTipImage32by32Top", this.ToolTipImage32by32Top);
                XmlHelper.AddAttribute(galleryButtonNode, "ToolTipShortcutKey", this.ToolTipShortcutKey);
                XmlHelper.AddAttribute(galleryButtonNode, "ToolTipTitle", this.ToolTipTitle);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("GalleryButton");
                node.Tag = this;
                node.ContextMenuStrip = ContextMenuStripHelper.BuildUICommandDefContextMenuStrip(node, null, true);

                parentNode.Nodes.Add(node);

                if (node.Parent.Nodes.Count == 1)
                {
                    node.ContextMenuStrip.Items[0].Enabled = false;
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
